﻿/*
 * QWine Queue Machine (Special build for Immigration Office)
 * Copyright (C) 2010 Wish Enterprise Development Team

 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 * Contact Us:
 * E-Mail   : contact@wishenterprise.com
 * 
 * */

using System;
using System.Data;
using Origami.Data;
using Origami.Data.Provider;
using QWEntity;

namespace QWRepository
{
    /// <summary>
    /// ServerInfoRepository: Repository
    /// </summary>
    public class ServerInfoRepository : DataAccess
    {
        private readonly DataSource _dataSource;
        private readonly IEntityManager _em;

        /// <summary>
        /// Initializes a new instance of the <see cref="ServerInfoRepository"/> class.
        /// </summary>
        public ServerInfoRepository()
        {
            try
            {
                _dataSource = GetDataSource("LOCAL");
                _em = new EntityManager(_dataSource);
            }
            catch (NullReferenceException ex)
            {
                throw new Exception(string.Format("Unable to find Database Connection configuration files!\r\n{0}",
                                                  ex.Message));
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// Gets the server info.
        /// </summary>
        /// <returns></returns>
        public ServerInfo GetServerInfo()
        {
            string sql = "";
            var si = new ServerInfo();

            switch (_dataSource.Provider)
            {
                case "MySql.Data.MySqlClient":
                    sql = "SELECT SYSDATE() serverdate;";
                    break;
                case "System.Data.OracleClient":
                    sql = "SELECT SYSDATE AS serverdate FROM dual;";
                    break;
                case "System.Data.SqlClient":
                    sql = "SELECT GETDATE() serverdate;";
                    break;
                case "Npgsql":
                    sql = "SELECT NOW() serverdate;";
                    break;
            }

            DateTime dtNow = DateTime.Now;
            si.Information = "Offline";

            if (!string.IsNullOrEmpty(sql))
            {
                try
                {
                    using (IDataReader reader = _em.ExecuteReader(sql))
                    {
                        if (reader.Read())
                        {
                            si.Information = "Online";
                            dtNow = reader.GetDateTime(0);
                        }
                    }
                }
                catch
                {
                    dtNow = DateTime.Now;
                    si.Information = "Offline";
                }
            }

            si.Now = dtNow;
            return si;
        }
    }
}